package com.chenjt.netty.eight_nio.simple;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/20 10:18
 */
public class NioTest3 {

    public static void main(String[] args) throws Exception {

        FileOutputStream fileOutputStream = new FileOutputStream("src/main/java/com/chenjt/netty/eight_nio/simple/NioTest3.txt");
        FileChannel fileChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        byte[] messages = "hello world welcome,nihao".getBytes(StandardCharsets.UTF_8);

        for(int i = 0;i<messages.length;++i){
            byteBuffer.put(messages[i]);
        }

        byteBuffer.flip();

        // 将 messages 写入到项目根目录的 NioTest3.txt 文件中
        fileChannel.write(byteBuffer);
        fileOutputStream.close();


    }

}
